package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.AggressService;
import com.qianfeng.es.utils.ConnectionUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.range.ParsedRange;
import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/2/21/20:55
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class AggressServiceImpl implements AggressService {
    RestHighLevelClient client = ConnectionUtil.getClient();


    @Override
    public void cardinalityAgg(String index,String field) throws Exception {
//        SearchRequest request = new SearchRequest(index);
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.aggregation(AggregationBuilders.cardinality("bieming").field(field));//设置聚合函数
//        request.source(sourceBuilder);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        Aggregations aggregations = response.getAggregations();//获取到聚合的返回结果
        Aggregations aggregations = baseAggress(index, AggregationBuilders.cardinality("bieming").field(field));
        ParsedCardinality bieming = aggregations.get("bieming");
        System.err.println(bieming);
        System.err.println(bieming.value());
    }

    @Override
    public void rangeAgg(String index) throws Exception {
        Aggregations aggregations = baseAggress(index, AggregationBuilders.range("bieming").field("fee").addUnboundedTo(5).addUnboundedFrom(10).addRange(5, 10));
        ParsedRange bieming = aggregations.get("bieming");
        System.err.println(bieming);
        for (Range.Bucket bucket : bieming.getBuckets()) {
            System.err.println(bucket.getKeyAsString()+"---->"+bucket.getDocCount());
        }
    }

    @Override
    public void date_rangeAgg(String index) {
//        AggregationBuilders.dateRange("bieming").field("createDate")
//        AggregationBuilders.avg("bieming").field("fee");
//        AggregationBuilders.max("bieming").field("fee");
    }

    @Override
    public void ip_rangeAgg(String index) {

    }


    public Aggregations baseAggress(String index,AggregationBuilder builder) throws Exception {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.aggregation(builder);//设置聚合函数
        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        Aggregations aggregations = response.getAggregations();//获取到聚合的返回结果
        return aggregations;
    }
}
